package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.common.QueryCpUtil;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.OmsDlShareStockIncrQueryCmd;
import com.jackrain.nea.fc.dl.mapper.OmsDlShareStockMapper;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;

/**
 * 共享库存 增量
 *
 * @author kangcong
 * @date 2018/09/28
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class OmsDlShareStockIncrQueryCmdImpl extends CommandAdapter implements OmsDlShareStockIncrQueryCmd {
    @Autowired
    QueryCpUtil queryCpUtil;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
//        log.debug("--------------共享同步到OMS增量传输---start------------");
        ValueHolder vh = new ValueHolder();

        if (map == null) {
            vh.put("code", "-1");
            vh.put("msg", "请求参数为空");
            return vh;
        }

        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(
                map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("OmsDlShareStockIncrQueryCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！" + map.toString());
        }

        Object[] ids = param.getJSONArray("IDS").toArray();
        String action = param.getString("action");
        if (null == action || "".equals(action)  || (!"add".equals(action) && !"save".equals(action))) {
            vh.put("code", "-1");
            vh.put("msg", "不是add和save动作不处理"+map.toString());
            return vh;
        }

        OmsDlShareStockMapper omsDlShareStockMapper = ApplicationContextHandle.getBean(OmsDlShareStockMapper.class);
        JSONArray jsonArray = new JSONArray();

        //获取并处理库存数据
        List<HashMap> listData = omsDlShareStockMapper.selectDlShareStockChange(ids);

        for (HashMap hashMap : listData) {
            JSONObject js = new JSONObject(hashMap);
            JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                    "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param1);
        }
        log.debug("OmsDlShareStockIncrQueryCmdImpl--end-->{}",jsonArray);
//        log.debug("--------------共享同步到OMS增量传输---end------------"+jsonArray);

        vh.put("data", jsonArray);
        vh.put("code", 0);
        vh.put("message", "查询成功！");
        return vh;
    }
}
